﻿@model ProductReviewHelpfulnessModel
@using Nop.Web.Models.Catalog;
<div class="product-review-helpfulness">
    <span class="question">@T("Reviews.Helpfulness.WasHelpful?")</span>
    <span>
        <span id="vote-yes-@(Model.ProductReviewId)" class="vote text-success"><strong>@T("Common.Yes")</strong></span>&nbsp;<span>/</span>
        <span id="vote-no-@(Model.ProductReviewId)" class="vote text-warning"><strong>@T("Common.No")</strong></span>
    </span>
    <span>
        (<span id="helpfulness-vote-yes-@(Model.ProductReviewId)">@(Model.HelpfulYesTotal)</span>/<span id="helpfulness-vote-no-@(Model.ProductReviewId)">@(Model.HelpfulNoTotal)</span>)
    </span>
    <div id="helpfulness-vote-result-@(Model.ProductReviewId)" class="result text-info"></div>

    
    <script type="text/javascript">
        $(document).ready(function () {
            $('#vote-yes-@(Model.ProductReviewId)').click(function () {
                setProductReviewHelpfulness@(Model.ProductReviewId)('true');
            });
            $('#vote-no-@(Model.ProductReviewId)').click(function () {
                setProductReviewHelpfulness@(Model.ProductReviewId)('false');
            });
        });

        function setProductReviewHelpfulness@(Model.ProductReviewId)(wasHelpful){
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@(Url.RouteUrl("SetProductReviewHelpfulness"))",
                    data: { "productReviewId": @(Model.ProductReviewId), "washelpful": wasHelpful },
                    success: function (data) {
                         $("#helpfulness-vote-yes-@(Model.ProductReviewId)").html(data.TotalYes);
                         $("#helpfulness-vote-no-@(Model.ProductReviewId)").html(data.TotalNo);
                         $("#helpfulness-vote-result-@(Model.ProductReviewId)").html(data.Result);
                                                  
                         $('#helpfulness-vote-result-@(Model.ProductReviewId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to vote. Please refresh the page and try one more time.');
                    }  
                });
        }
    </script>
</div>
